package pl.lodz.p.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Transient;

import pl.lodz.p.model.enums.Gender;


@Entity(name="Patient")
public class Patient{

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	
	@Column(name="firstName", nullable=true)
	private String firstName;

	@Column(name="middleName")
	private String middleName;

	@Column(name="lastName", nullable=true)
	private String lastName;

	@Column(nullable=true)
	private String pesel;
	
	@Column(nullable=true)
	private String nip;
	
	@Column(nullable=true)
	private Date birthDate;
	
	@Enumerated(EnumType.STRING)
	private Gender gender;
	
	@Column(nullable=true)
	private String mobilePhone;

	@Column(nullable=true)
	private String email;
	
	@Embedded
    private ContactPerson contact;
	
	@Column
	private Boolean contactIdentical = false;

	@Embedded
    private Address address;
	
	@Transient
	private int age;
	
	@ManyToOne
	@PrimaryKeyJoinColumn
	private User user;
	
	@OneToMany(mappedBy="patient", fetch=FetchType.LAZY)
	private List<Visit> visits;
	
	public Patient() {
		super();
		contact = new ContactPerson();
		address = new Address();
	}
	
	public List<Visit> getVisits() {
		return visits;
	}
	
	public void setVisits(List<Visit> visits) {
		this.visits = visits;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPesel() {
		return pesel;
	}

	public void setPesel(String pesel) {
		this.pesel = pesel;
	}

	public String getNip() {
		return nip;
	}

	public void setNip(String nip) {
		this.nip = nip;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public ContactPerson getContact() {
		return contact;
	}

	public void setContact(ContactPerson contact) {
		this.contact = contact;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@SuppressWarnings("deprecation")
	public int getAge() {
		return new Date().getYear()-birthDate.getYear();
	}

	public void setAge(int age) {
		this.age = age;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the contactIdentical
	 */
	public boolean isContactIdentical() {
		return contactIdentical.booleanValue();
	}

	/**
	 * @param contactIdentical the contactIdentical to set
	 */
	public void setContactIdentical(boolean contactIdentical) {
		this.contactIdentical = new Boolean(contactIdentical);
	}
	
	public boolean isContactNeeded(){
		return contactIdentical.booleanValue();
	}
	
	public String getFullName() {
		return firstName + " " + lastName;
	}
	
	public String getFullNameHidden() {
		return firstName.substring(0, 1) + "****** " + lastName.substring(0, 1) + "****** ";
	}
}
